﻿//
// © Algobox Ltd - Benn Eichhorn
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Algobox.Objects.Finance.Assets.Stock;
using System.Collections.Concurrent;

namespace Algobox.Structure.Finance.Strategies.SmartStoploss
{
    public class SmartStoplossManagement
    {
        #region Constructors

        public SmartStoplossManagement(Algobox.Feeds.Finance.MarketData.IMarketFeedAdaptor marketFeed,
            Algobox.Feeds.Finance.Trading.ITradingFeedAdaptor tradingFeed)
        {
            MarketFeed = marketFeed;
            TradingFeed = tradingFeed;            
        }

        #endregion
//////////////////
        #region Member Variables

        ConcurrentDictionary<StockListing, SmartStoplossStock> _mapStocks = new ConcurrentDictionary<StockListing, SmartStoplossStock>();

        #endregion
//////////////////
        #region Properties

        public Algobox.Feeds.Finance.MarketData.IMarketFeedAdaptor MarketFeed { get; private set; }
        public Algobox.Feeds.Finance.Trading.ITradingFeedAdaptor TradingFeed { get; private set; }
        
        #endregion
//////////////////
        #region Functions

        public SmartStoplossOrder CreateSmartStoploss(Strategy strategy, ISmartStoplossCallback callback, string account, StockListing stock, bool isBuy, double priceTarget, double priceStop, uint quantity)
        {
            SmartStoplossOrder order = null; 
            _mapStocks.AddOrUpdate(stock,
                (key) =>
                {
                    SmartStoplossStock manager = new SmartStoplossStock(this, key);
                    order = new SmartStoplossOrder(manager, strategy, account, isBuy, priceTarget, priceStop, quantity);
                    manager.AddOrder(order, callback);
                    return manager;
                },
                (key, value) =>
                {
                    order = new SmartStoplossOrder(value, strategy, account, isBuy, priceTarget, priceStop, quantity);
                    value.AddOrder(order, callback);
                    return value;
                });
            return order;
        }

        #endregion
//////////////////
        #region Enums



        #endregion
    }
}
